﻿#region | Using |
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.ComponentModel;
using System.Web.Script.Serialization;
#endregion

namespace Sobiens.Web.Controls.ItemSelector
{
    /// <summary>
    /// Summary description for ServerControl1
    /// </summary>
    public class ItemSelectorControl : ScriptControl
    {
        #region | Members - Private |
        HiddenField SelectedItemsHiddenField;
        TextBox ItemNameTextBox;
        HyperLink AdvancedSelectionHyperLink;
        HtmlGenericControl ItemTable;
        HtmlGenericControl ItemPickerPanel;
        #endregion

        #region | Properties - Public |
        public string AutoCompleteWSUrl
        {
            get;
            set;
        }

        public string SelectorImageUrl
        {
            get;
            set;
        }

        public string SelectorUrl
        {
            get;
            set;
        }

        [
        Browsable(false),
        DefaultValue(null),
        Description("The item picker template."),
        PersistenceMode(PersistenceMode.InnerProperty)
        ]
        public virtual PlaceHolder ItemPickerTemplate
        {
            get;set;
        }

        public List<Item> SelectedItems
        {
            get
            {
                string selectedItems = this.Page.Request[this.ClientID + "_SelectedItemsHiddenField"];
                List<Item> items = new List<Item>();
                if (string.IsNullOrEmpty(selectedItems) == false)
                {
                    JavaScriptSerializer objJSSerializer = new JavaScriptSerializer();
                    items = objJSSerializer.Deserialize<List<Item>>(selectedItems);
                }
                return items;
            }
        }
        #endregion

        #region | Constructors |
        public ItemSelectorControl()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        #endregion

        #region | Methods - Protected |
        protected override IEnumerable<ScriptDescriptor>
                GetScriptDescriptors()
        {
            ScriptControlDescriptor desc = new ScriptControlDescriptor("Sobiens.Web.Controls.ItemSelector.ClientControl1", this.ClientID);

            desc.AddProperty("autoCompleteWSUrl", this.AutoCompleteWSUrl);

            return new ScriptDescriptor[] { desc };
        }

        // Generate the script reference
        protected override IEnumerable<ScriptReference>
                GetScriptReferences()
        {
            yield return new ScriptReference("Sobiens.Web.Controls.ItemSelector.ClientControl1.js", this.GetType().Assembly.FullName);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            SelectedItemsHiddenField = new HiddenField();
            SelectedItemsHiddenField.ID = this.ClientID + "_SelectedItemsHiddenField";
            SelectedItemsHiddenField.EnableViewState = true;
            SelectedItemsHiddenField.Value = this.Page.Request[this.ClientID + "_SelectedItemsHiddenField"];

            ItemNameTextBox = new TextBox();
            ItemNameTextBox.ID = this.ClientID + "_ItemNameTextBox";
            ItemNameTextBox.EnableViewState = true;
            ItemNameTextBox.Text = this.Page.Request[this.ClientID + "_ItemNameTextBox"];

            AdvancedSelectionHyperLink = new HyperLink();
            AdvancedSelectionHyperLink.ID = this.ClientID + "_AdvancedSelectionHyperLink";
            AdvancedSelectionHyperLink.Text = string.Format(@"<img src='{0}' align='absmiddle' border='0'>", SelectorImageUrl);
            AdvancedSelectionHyperLink.NavigateUrl = this.SelectorUrl; 
            //AdvancedSelectionHyperLink.Attributes["onclick"] = "javascript:openItemSelectionWindow()";

            ItemTable = new HtmlGenericControl("div");
            ItemTable.ID = this.ClientID + "_ItemTable";

            ItemPickerPanel = new HtmlGenericControl("div");
            ItemPickerPanel.ID = this.ClientID + "_ItemPickerPanel";
            ItemPickerPanel.Attributes["title"] = "Item Selection";

            this.Controls.Add(SelectedItemsHiddenField);
            this.Controls.Add(ItemNameTextBox);
            this.Controls.Add(AdvancedSelectionHyperLink);
            this.Controls.Add(ItemTable);
            ItemPickerPanel.Controls.Add(ItemPickerTemplate);
            this.Controls.Add(ItemPickerPanel);

            object z = SelectedItems;
        }
        #endregion
    }
}